package com.landeqing.dao.impl;

import com.landeqing.dao.StudentDao;
import com.landeqing.model.BanJi;
import com.landeqing.model.Student;
import com.landeqing.utils.MyBatisUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class StudentDaoImpl implements StudentDao {

    public boolean insert(Student student) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            int result = sqlSession.insert("com.landeqing.model.studentModel.add",student);
            if (result>0)
            sqlSession.commit();
            sqlSession.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<Student> findAll(int pageNo,int pageSize){

        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            RowBounds rowBounds=new RowBounds((pageNo-1)*pageSize,pageSize);
            List<Student> list=sqlSession.selectList("com.landeqing.model.studentModel.find",null,rowBounds);
            sqlSession.commit();
            sqlSession.close();
            return list;
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    public Student getById(Integer id) {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            Student  student= sqlSession.selectOne("com.landeqing.model.studentModel.getById",id);
         sqlSession.commit();
         sqlSession.close();
         return student;
        }catch (Exception e){
            e.getMessage();
        }
        return null;
    }

    public int getTotalCount() {
        try {
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
          int count=  sqlSession.selectOne("com.landeqing.model.studentModel.getTotalCount");
          sqlSession.close();
          return count;
        }catch (Exception e){
            e.getMessage();
        }
        return 0;
    }
    //    public  static void  main (String [] args){
//        StudentDaoImpl studentDao=new StudentDaoImpl();
//        Student student=studentDao.getById(3);
//        System.out.println(student.getId()+"  "+student.getName()+"  "+student.getBanji().getClassName());
//    }
}
